package private

import (
	"dc-go/global"
	"dc-go/model"
)

type ServiceSqlSqlparam struct{}

func (s *ServiceSqlSqlparam) GetSqlSqlparamList(sqlCode *model.RequestSqlCode) (sqlSqlparam []model.SysSqlSqlparam, err error) {
	err = global.DcDb.Where("sql_code=?", sqlCode.SqlCode).Find(&sqlSqlparam).Error
	return sqlSqlparam, err
}

func (s *ServiceSqlSqlparam) EditSqlSqlparam(toEditSqlSqlparam *model.RequestSqlSqlparamEdit2, empeeAcct string) (err error) {
	// var sqlparam model.SysSqlparam
	// err = global.DcDb.Where("sqlparam_code = ?", toEditSqlparamMenu.SqlparamCode).First(&sqlparam).Error
	// if err != nil {
	// 	return err
	// }
	var sqlSqlparamsBefore []model.SysSqlSqlparam
	err = global.DcDb.Where("sql_code = ?", toEditSqlSqlparam.SqlCode).Find(&sqlSqlparamsBefore).Error
	for _, sqlparamCode := range toEditSqlSqlparam.SqlparamCode {
		//如果是新增的菜单，则插入数据
		var isExist bool
		for _, sqlSqlparamBefore := range sqlSqlparamsBefore {
			if sqlSqlparamBefore.SqlparamCode == sqlparamCode {
				isExist = true
				break
			}
		}
		if !isExist {
			var sqlSqlparam model.SysSqlSqlparam
			sqlSqlparam.SqlCode = toEditSqlSqlparam.SqlCode
			sqlSqlparam.SqlparamCode = sqlparamCode
			err = global.DcDb.Create(&sqlSqlparam).Error
			if err != nil {
				return err
			}
		}
	}
	//删除旧的菜单
	for _, sqlSqlparamBefore := range sqlSqlparamsBefore {
		var isExist bool
		for _, sqlparamCode := range toEditSqlSqlparam.SqlparamCode {
			if sqlSqlparamBefore.SqlparamCode == sqlparamCode {
				isExist = true
				break
			}
		}
		if !isExist {
			err = global.DcDb.Where("sql_code = ? and sqlparam_code = ?", toEditSqlSqlparam.SqlCode, sqlSqlparamBefore.SqlparamCode).Delete(&model.SysSqlSqlparam{}).Error
			if err != nil {
				return err
			}
		}
	}
	return err
}
